


package com.glp.work.flow.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @title 数据源配置类
 */
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    
}


//package com.glp.work.flow.config;
//
//import java.util.Properties;
//
//import javax.transaction.SystemException;
//
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.transaction.jta.JtaTransactionManager;
//
//import com.atomikos.icatch.jta.UserTransactionImp;
//import com.atomikos.icatch.jta.UserTransactionManager;
//
///**
// * @author liwenhong
// * @title 多数据源配置类
// * @date 2018/11/1.
// */
//@Configuration
//public class DataSourceConfig {
//
//    @Value("${spring.datasource.url}")
//    private String businessDBUrl;
//
//    @Value("${spring.datasource.username}")
//    private String businessDBUserName;
//
//    @Value("${spring.datasource.password}")
//    private String businessDBPassWord;
//
//    @Value("${spring.datasource.minPoolSize}")
//    private int businessDBMinPoolSize;
//
//    @Value("${spring.datasource.maxPoolSize}")
//    private int businessDBMaxPoolSize;
//
//    @Value("${spring.datasource.connectionTimeout}")
//    private int businessDBConnectionTimeout;
//
//    @Value("${spring.datasource.idleTimeout}")
//    private int businessDBIdleTimeout;
//
//    @Value("${spring.datasource.maxLifetime}")
//    private int businessDBMaxLifetime;
//
//
//    @Value("${spring.datasource1.activiti.url}")
//    private String activitiDBUrl;
//
//    @Value("${spring.datasource1.activiti.username}")
//    private String activitiDBUserName;
//
//    @Value("${spring.datasource1.activiti.password}")
//    private String activitiDBPassWord;
//
//    @Value("${spring.datasource1.activiti.minPoolSize}")
//    private int activitiDBMinPoolSize;
//
//    @Value("${spring.datasource1.activiti.maxPoolSize}")
//    private int activitiDBMaxPoolSize;
//
//    @Value("${spring.datasource1.activiti.connectionTimeout}")
//    private int activitiDBConnectionTimeout;
//
//    @Value("${spring.datasource1.activiti.idleTimeout}")
//    private int activitiDBIdleTimeout;
//
//    @Value("${spring.datasource1.activiti.maxLifetime}")
//    private int activitiDBMaxLifetime;
//
//
//
//    /**
//     * 业务数据专用 datasource
//     *
//     * @return
//     */
//    @Bean
//    @Primary
//    @Qualifier("businessDB")
//    @ConfigurationProperties(prefix = "spring.datasource")
//    public AtomikosDataSourceBean businessDBDataSourceBean() {
//        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
//        atomikosDataSourceBean.setUniqueResourceName("businessDB");
//        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
//        Properties properties = new Properties();
//        properties.put("URL", businessDBUrl);
//        properties.put("user", businessDBUserName);
//        properties.put("password", businessDBPassWord);
//        atomikosDataSourceBean.setMinPoolSize(businessDBMinPoolSize);
//        atomikosDataSourceBean.setMaxPoolSize(businessDBMaxPoolSize);
//        atomikosDataSourceBean.setReapTimeout(businessDBConnectionTimeout);
//        atomikosDataSourceBean.setMaxIdleTime(businessDBIdleTimeout);
//        atomikosDataSourceBean.setMaxLifetime(businessDBMaxLifetime);
//        atomikosDataSourceBean.setXaProperties(properties);
//        return atomikosDataSourceBean;
//    }
//
//    /**
//     * 工作流专用 datasource
//     *
//     * @return
//     */
//
//    @Bean
//    @Qualifier("activitiDB")
//    @ConfigurationProperties(prefix = "spring.datasource1.activiti")
//    public AtomikosDataSourceBean activitiDBDataSourceBean() {
//        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
//        atomikosDataSourceBean.setUniqueResourceName("activitiDB");
//        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
//        Properties properties = new Properties();
//        properties.put("URL", activitiDBUrl);
//        properties.put("user", activitiDBUserName);
//        properties.put("password", activitiDBPassWord);
//        atomikosDataSourceBean.setXaProperties(properties);
//        atomikosDataSourceBean.setMinPoolSize(activitiDBMinPoolSize);
//        atomikosDataSourceBean.setMaxPoolSize(activitiDBMaxPoolSize);
//        atomikosDataSourceBean.setReapTimeout(activitiDBConnectionTimeout);
//        atomikosDataSourceBean.setMaxIdleTime(activitiDBIdleTimeout);
//        atomikosDataSourceBean.setMaxLifetime(activitiDBMaxLifetime);
//        return atomikosDataSourceBean;
//    }
//
//
//    /**
//     * transaction manager
//     *
//     * @return
//     */
//    @Bean(destroyMethod = "close", initMethod = "init")
//    public UserTransactionManager userTransactionManager() {
//        UserTransactionManager userTransactionManager = new UserTransactionManager();
//        userTransactionManager.setForceShutdown(false);
//        return userTransactionManager;
//    }
//
//    @Bean
//    public UserTransactionImp userTransactionImp() throws SystemException {
//        UserTransactionImp userTransactionImp=new UserTransactionImp();
//        userTransactionImp.setTransactionTimeout(3000);
//        return userTransactionImp;
//    }
//
//    /**
//     * jta transactionManager
//     *
//     * @return
//     */
//    @Bean
//    public JtaTransactionManager transactionManager() throws SystemException {
//        JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
//        jtaTransactionManager.setTransactionManager(userTransactionManager());
//        jtaTransactionManager.setUserTransaction(userTransactionImp());
//        return jtaTransactionManager;
//    }
//}
